package core.query;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页类
 * 
 * @author Administrator
 * 
 */
public class Pagination implements Serializable {

	private static final long serialVersionUID = 6835934988813053949L;

	/**
	 * 本页在分页中的索引数
	 */
	private int activePagination;

	/**
	 * 本页开始索引
	 */
	private int offset;

	/**
	 * 每页偏移量
	 */
	private int rowCount;

	/**
	 * 默认显示多少页码
	 */
	private int paginationScope;

	/**
	 * 第一页
	 */
	private int firstPage;

	/**
	 * 最后一页
	 */
	private int lastPage;

	private List<Integer> pageList = new ArrayList<Integer>();
	
	private static final int FIRST_PAGE_INDEX = 1;
	
	private static final int ROW_COUNT = 10;
	
	private static final int PAGINATION_SCOPE = 11;

	private Pagination() {
		super();
		this.activePagination = FIRST_PAGE_INDEX;
		this.rowCount = ROW_COUNT;
		this.paginationScope = PAGINATION_SCOPE;
		this.offset = calOffset(this.activePagination);
		setPaginationScope();
	}

	private Pagination(int activePagination) {
		super();
		this.activePagination = activePagination;
		this.rowCount = ROW_COUNT;
		this.paginationScope = PAGINATION_SCOPE;
		this.offset = calOffset(this.activePagination);
		setPaginationScope();
	}

	public static Pagination newInstance(int activePagination) {
		if (activePagination < 1) {
			activePagination = 1;
		}
		return new Pagination(activePagination);
	}

	public static Pagination newPaginationScope(Pagination pagination) {
		Pagination pagination2 = newInstance(1);
		pagination2.setOffset(pagination.getNextOffset());
		pagination2.setRowCount(pagination.getPaginationCount());
		return pagination2;
	}

	/**
	 * 设置页面默认值
	 */
	private void setPaginationScope() {
		this.firstPage = this.activePagination - (this.paginationScope / 2);
		if (this.firstPage < 1) {
			this.firstPage = 1;
		}
		this.lastPage = this.firstPage + this.paginationScope - 1;
	}

	/**
	 * 计算下一页的偏移量
	 * 
	 * @return
	 */
	public int getNextOffset() {
		return calOffset(this.activePagination + 1);
	}

	/**
	 * 计算需要查询的总数
	 * 
	 * @return
	 */
	public int getPaginationCount() {
		return (this.lastPage - this.activePagination) * this.rowCount;
	}

	/**
	 * 根据页数计算页面范围
	 * 
	 * @param count
	 */
	public void setLastPage(int count) {
		if (count < 0) {
			throw new IllegalArgumentException("参数非法");
		}
		int pages = 0;
		if (count % this.rowCount > 0) {
			pages = count / this.rowCount + 1;
		} else {
			pages = count / this.rowCount;
		}
		this.lastPage = this.activePagination + pages;
		this.firstPage = this.lastPage - this.paginationScope + 1;
		if (this.firstPage < 1) {
			this.firstPage = 1;
		}
		for (int i = this.firstPage; i < this.lastPage + 1; i++) {
			this.pageList.add(i);
		}
	}

	/**
	 * 计算每页的偏移量
	 * 
	 * @param pagination
	 * @return
	 */
	private int calOffset(int pagination) {
		return (pagination - 1) * this.rowCount;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public boolean isFirstPage() {
		return this.activePagination == 1;
	}

	public boolean isLastPage() {
		return this.lastPage == this.activePagination;
	}

	public boolean isActivePagination(int page) {
		return this.activePagination == page;
	}

	public int getPrevPage() {
		return this.activePagination - 1;
	}

	public int getNextPage() {
		return this.activePagination + 1;
	}

	public List<Integer> getPageList() {
		return pageList;
	}

	public int getActivePagination() {
		return activePagination;
	}

	public int getOffset() {
		return offset;
	}

	public int getRowCount() {
		return rowCount;
	}

	public int getPaginationScope() {
		return paginationScope;
	}

	public int getFirstPage() {
		return firstPage;
	}

	public int getLastPage() {
		return lastPage;
	}

}
